<?php

namespace mpend\modules\home\models;

use Yii;

/**
 * 需求分类栏目检索
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class RecommendSearch extends \mpend\models\searchs\ArticleSearch {

    public $recent = false;

    /**
     * @inheritdoc
     */
    public function buildQuery($alias = 't') {
        $query = static::find();
        if ($alias) {
            $query->alias($alias);
            $alias .= '.';
        }
        $query->innerJoinWith('channel channel');
        $query->andWhere([
            '!=', 'channel.scope', 'match'
        ]);
        $query->active(static::STATUS_ACTIVE);
        if ($this->recent) {
            $query->orderBy([
                $alias . 'created_at' => SORT_DESC,
            ]);
        } else {
            $query->orderBy([
                $alias . 'recommend_id' => SORT_DESC,
                $alias . 'publish_at' => SORT_DESC,
                $alias . 'top_at' => SORT_DESC,
            ]);
        }
        return $query;
    }

}
